﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using YT.DB;
using YT.Commons;
using YT.Modules; 


namespace YT.XH.SuddenDeafness
{
    public partial class frmSDSickHistory : Form
    {
        private string mstrSickRecID = string.Empty;
        private ArrayList marrlist = new ArrayList();
        public frmSDSickHistory()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmSDSickHistory(string strSickRecID)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }
        private void SelectNextText(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SelectNextControl((Control)sender, true, true, true, true);
        }
        private void FindPanel(Control cons)
        {
            DBAccess dbcon =new DBAccess (false,false );
            foreach (Control con in cons.Controls)
            {                
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "RadioButton") ((RadioButton)con).CheckedChanged += new EventHandler(this.CalScore); 
            }
            dbcon.CloseCon ();
        }

        private void CalScore(object sender, EventArgs e)
        {
            CalScoreAndSub();
            CalTHI(); 
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (UpdateData()) this.Close();
            }
            else 
            this.Close(); 
        }

        private void frmSDSickHistory_Load(object sender, EventArgs e)
        {
            this.Text = SysVar.CurrentMenuInfo.strMenuName; 
            FillData();
            timer1.Start();
        }

        private void FillData()
        {

            string strTableName = "titleResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
                
            }

            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.SuddenDeafness.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='"+ mstrSickRecID  +"' and Type='" + TitleType.SuddenDeafness  + "'";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) continue ;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox" || conText.GetType().Name == "ComboBox"    )
                            {
                                conText.Text = strResult;
                                if (conText.Text  != "") helpProd.SetHelpString(conText, conText.Text);
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                ((DateTimePicker)conText).Value = CvtFunc.ToDateTime(strResult);
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                continue;
                            }
                        }
                    }

                }
            }
            dbcon.CloseCon();
        }
       
         private bool  UpdateData()
        {

            CalTHI();
            CalScoreAndSub();
            string strTableName="titleResult";
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox" || conText.GetType().Name == "ComboBox")
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "DateTimePicker")
                            {
                                strResult = BaseFunctions.FormatDate(((DateTimePicker)conText).Value.ToString() , DateFormatOption.DateFmtYMD); 
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                continue;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + strTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and SickRecID='"+ mstrSickRecID  +"' and Type='" + TitleType.SuddenDeafness  + "'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + strTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + strTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.SuddenDeafness ;                            
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, strTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
            return true;
        }

         private void radioButton5_CheckedChanged(object sender, EventArgs e)
         {

         }

         private void btnSave_Click(object sender, EventArgs e)
         {
             if (!CheckInput()) return;
             if (UpdateData()) this.Close ();
         }

         private void flpPanel_Paint(object sender, PaintEventArgs e)
         {

         }

         private void btnJocobon_Click(object sender, EventArgs e)
         {
             frmSDJacobsonTitle frmJacob = new frmSDJacobsonTitle();
             frmJacob.ShowDialog();
         }

         private void button1_Click(object sender, EventArgs e)
         {

         }


         private void CalTHI()
         {
             int intResult = 0;
             int intFscore = 0;
             int intEscore = 0;
             int intCscore = 0;
             int intTotalScore = 0;
             int intTotalSubScore = 0;
             foreach (Control conDtl in marrlist)
             {
                 if (conDtl.GetType().Name == "Panel")
                 {
                     if (conDtl.Name.Substring(0, 5) == "pnl2_")
                     {
                         string strType = conDtl.Name.Split('_')[1].Substring(0, 1);
                         foreach (Control conItem in conDtl.Controls)
                         {
                             if (conItem.GetType().Name == "RadioButton")
                             {
                                 if (((RadioButton)conItem).Checked)
                                 {
                                     intResult = BaseFunctions.GetTHIScoreFunction(conItem.Tag.ToString());
                                 }
                                 if (strType == "E") intEscore = intEscore + intResult;
                                 if (strType == "F") intFscore = intFscore + intResult;
                                 if (strType == "C") intCscore = intCscore + intResult;
                                 intResult = 0;
                             }
                         }
                     }
                 }
             }

             intTotalScore = intFscore + intEscore + intCscore;
             txtTHIEscroe.Text = CvtFunc.ToString(intEscore);
             txtTHICscore.Text = CvtFunc.ToString(intCscore);
             txtTHIFscore.Text = CvtFunc.ToString(intFscore);

             txtTHITotal.Text = CvtFunc.ToString(intTotalScore);

         }
         private void CalScoreAndSub()
         {
             int intResult = 0;
             int intFscore = 0;
             int intEscore = 0;
             int intPscore = 0;
             int intTotalScore = 0;
             int intTotalSubScore = 0;
             foreach (Control conDtl in marrlist)
             {
                 if (conDtl.GetType().Name == "Panel")
                 {
                     if (conDtl.Name.Substring(0, 4) == "pnl_")
                     {
                         string strType = conDtl.Name.Split('_')[1].Substring(0, 1);
                         foreach (Control conItem in conDtl.Controls)
                         {
                             if (conItem.GetType().Name == "RadioButton")
                             {
                                 if (((RadioButton)conItem).Checked)
                                 {
                                     intResult = BaseFunctions.GetScoreFunction(conItem.Tag.ToString());
                                 }
                                 if (strType == "E") intEscore = intEscore + intResult;
                                 if (strType == "F") intFscore = intFscore + intResult;
                                 if (strType == "P") intPscore = intPscore + intResult;
                                 intResult = 0;
                             }
                         }
                     }
                 }
             }

             intTotalScore = intFscore + intEscore + intPscore;
             txtEscore.Text = CvtFunc.ToString(intEscore);
             txtPscore.Text = CvtFunc.ToString(intPscore);
             txtFscore.Text = CvtFunc.ToString(intFscore);

             txtTotalScore.Text = CvtFunc.ToString(intTotalScore);


         }

         private void btnCalu_Click(object sender, EventArgs e)
         {
             CalScoreAndSub();
         }

         private void frmSDSickHistory_FormClosed(object sender, FormClosedEventArgs e)
         {
             SysVar.marrFormList.Remove(this.Name);
             frmGifImage frmGIF = new frmGifImage();
             frmGIF.TopMost = true;
             frmGIF.Show();
         }


         private void ValidCheck(object sender, EventArgs e)
         {
             Control con = sender as Control;
             string strConName = con.Name;

             #region 起病后听力下降最严重时发生的时间             

             if (strConName == textBox2.Name)
             {
                 if (con.Text != "")
                 {
                     radioButton4.Checked = true;
                     textBox3.Text = "";
                     textBox4.Text = "";
                     textBox5.Text = "";
                 }
             }
             else if (strConName == textBox3.Name)
             {
                 if (con.Text != "")
                 {
                     radioButton5.Checked = true;
                     textBox2.Text = "";
                     textBox4.Text = "";
                     textBox5.Text = "";
                 }
             }
             else if (strConName == textBox4.Name)
             {
                 if (con.Text != "")
                 {
                     radioButton6.Checked = true;
                     textBox2.Text = "";
                     textBox3.Text = "";
                     textBox5.Text = "";
                 }
             }
             else if (strConName == textBox5.Name)
             {
                 if (con.Text != "")
                 {
                     radioButton7.Checked = true;
                     textBox2.Text = "";
                     textBox3.Text = "";
                     textBox4.Text = "";
                 }
             }

             #endregion


             #region
             if (strConName == "textBox10")
             {
                 if (con.Text != "") radioButton65.Checked = true;
             }
             else if (strConName == radioButton66.Name) //否
             {
               textBox10.Text = "";                 
             }
             else if (strConName == radioButton65.Name) //是
             {
                
             }

             #endregion

             #region
             if (strConName == textBox9.Name)
             {
                 if ( textBox9.Text.Trim () !="")
                     radioButton72.Checked = true;
                 else
                     radioButton72.Checked = false;
             }

             if (strConName == radioButton72.Name) //是
             {
                 
             }
             else if (strConName == radioButton69.Name) //否
             {
                 textBox9.Text = "";
             }
             #endregion

             #region 其他遗传性疾病史

             if (strConName == textBox17.Name )
             {
                 if (textBox17.Text.Trim() != "")
                     radioButton77.Checked = true;
                 else
                     radioButton77.Checked = false;

             }
            

             if (strConName == radioButton77.Name) //是
             {

             }
             else if (strConName == radioButton78.Name) //否
             {
                 textBox17.Text = "";                 
             }

             #endregion

             #region 听力突然下降

             if (strConName == textBox18.Name || strConName == textBox19.Name || strConName == textBox20.Name)
             {
                 if (textBox18.Text.Trim() != "")
                     radioButton81.Checked = true;
                 else
                     radioButton81.Checked = false;
             }

             if (strConName == radioButton81.Name) //是
             {

             }
             else if (strConName == radioButton82.Name) //否
             {
                 textBox18.Text = "";
                 textBox19.Text = "";
                 textBox20.Text = "";
             }

             #endregion

             #region 其他听力问题

             if (strConName == textBox21.Name)
             {
                 if (textBox21.Text.Trim() != "")
                     radioButton83.Checked = true;
                 else
                     radioButton83.Checked = false;

             }

             if (strConName == radioButton83.Name) //是
             {

             }
             else if (strConName == radioButton84.Name) //否
             {
                 textBox21.Text = "";
             }

             #endregion


             #region 视力问题

             if (strConName == textBox22.Name)
             {
                 if (textBox22.Text.Trim() != "")
                     radioButton85.Checked = true;
                 else
                     radioButton85.Checked = false;

             }

             if (strConName == radioButton85.Name) //是
             {

             }
             else if (strConName == radioButton86.Name) //否
             {
                 textBox22.Text = "";
             }

             #endregion

             #region 食物过敏

             if (strConName == textBox23.Name)
             {
                 if (textBox23.Text.Trim() != "")
                     radioButton87.Checked = true;
                 else
                     radioButton87.Checked = false;

             }

             if (strConName == radioButton87.Name) //是
             {

             }
             else if (strConName == radioButton88.Name) //否
             {
                 textBox23.Text = "";
             }

             #endregion


             #region 吸入过敏

             if (strConName == textBox24.Name)
             {
                 if (textBox24.Text.Trim() != "")
                     radioButton89.Checked = true;
                 else
                     radioButton89.Checked = false;

             }

             if (strConName == radioButton89.Name) //是
             {

             }
             else if (strConName == radioButton90.Name) //否
             {
                 textBox24.Text = "";
             }

             #endregion


             #region 药物过敏

             if (strConName == textBox25.Name)
             {
                 if (textBox25.Text.Trim() != "")
                     radioButton91.Checked = true;
                 else
                     radioButton91.Checked = false;

             }

             if (strConName == radioButton91.Name) //是
             {

             }
             else if (strConName == radioButton92.Name) //否
             {
                 textBox25.Text = "";
             }

             #endregion


             #region 皮肤

             if (strConName == textBox27.Name)
             {
                 if (textBox27.Text.Trim() != "")
                     radioButton95.Checked = true;
                 else
                     radioButton95.Checked = false;

             }

             if (strConName == radioButton95.Name) //是
             {

             }
             else if (strConName == radioButton96.Name) //否
             {
                 textBox27.Text = "";
             }

             #endregion



             #region 关节

             if (strConName == textBox30.Name)
             {
                 if (textBox30.Text.Trim() != "")
                     radioButton101.Checked = true;
                 else
                     radioButton101.Checked = false;

             }

             if (strConName == radioButton101.Name) //是
             {

             }
             else if (strConName == radioButton102.Name) //否
             {
                 textBox30.Text = "";
             }

             #endregion 

             #region 过度疲劳

             if (strConName == textBox26.Name)
             {
                 if (textBox26.Text.Trim() != "")
                     radioButton117.Checked = true;
                 else
                     radioButton117.Checked = false;

             }

             if (strConName == radioButton117.Name) //是
             {

             }
             else if (strConName == radioButton118.Name) //否
             {
                 textBox26.Text = "";
             }

             #endregion 

             #region 精神紧张

             if (strConName == textBox28.Name)
             {
                 if (textBox28.Text.Trim() != "")
                     radioButton115.Checked = true;
                 else
                     radioButton115.Checked = false;

             }

             if (strConName == radioButton115.Name) //是
             {

             }
             else if (strConName == radioButton116.Name) //否
             {
                 textBox28.Text = "";
             }

             #endregion 


             #region 吸烟

             if (strConName == textBox31.Name)
             {
                 if (textBox31.Text.Trim() != "")
                     radioButton109.Checked = true;
                 else
                     radioButton109.Checked = false;

             }

             if (strConName == radioButton109.Name) //是
             {

             }
             else if (strConName == radioButton110.Name) //否
             {
                 textBox31.Text = "";
             }

             #endregion 


             #region 饮酒

             if (strConName == textBox29.Name)
             {
                 if (textBox29.Text.Trim() != "")
                     radioButton97.Checked = true;
                 else
                     radioButton97.Checked = false;

             }

             if (strConName == radioButton97.Name) //是
             {

             }
             else if (strConName == radioButton98.Name) //否
             {
                 textBox29.Text = "";
             }

             #endregion 


             #region 过去是否有噪音接触史

             if (strConName == textBox33.Name )
             {
                 if (textBox33.Text.Trim() != "")
                     radioButton113.Checked = true;
                 else
                     radioButton113.Checked = false;

             }

             if (strConName == radioButton113.Name) //是
             {

             }
             else if (strConName == radioButton114.Name) //否
             {
                 textBox33.Text = "";
                 textBox32.Text = "";
             }

             #endregion 


             #region (8)其他用药情况

             if (strConName == textBox52.Name)
             {
                 if (textBox52.Text.Trim() != "")
                     radioButton121.Checked = true;
                 else
                     radioButton121.Checked = false;

             }

             if (strConName == radioButton121.Name) //是
             {

             }
             else if (strConName == radioButton122.Name) //否
             {
                 textBox52.Text = "";
                 textBox54.Text = "";
             }

             #endregion 

             #region 12、就诊时距最初发病时间


             if (strConName == textBox38.Name)
             {
                 if (con.Text != "") textBox39.Text = "";
             }
             else if (strConName == textBox39.Name)
             {
                 if (con.Text != "") textBox38.Text = "";
             }

             #endregion

             
         }

        private bool CheckInput()            
        {
             if (radHasTHI.Checked)
             {
                 if (txtTHITotal.Text == "" || CvtFunc.ToInt(txtTHITotal.Text) ==0)
                 {
                     MessageBox.Show("必须填写 耳鸣致残量表 。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     return false;
                 }
             }
             if (radJacobson.Checked)
             {
                 if (txtTotalScore.Text == "" || CvtFunc.ToInt(txtTotalScore.Text) == 0)
                 {
                     MessageBox.Show("必须填写 眩晕问卷和量化评估表 。", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     return false;
                 }
             }
             return true;
        }

         private void timer1_Tick(object sender, EventArgs e)
         {
             if (this.Opacity >= 1)
                 timer1.Stop();
             this.Opacity += 0.04;
         }

         private void toolStripButton1_Click(object sender, EventArgs e)
         {
             FormPrinting.FormPrinting  frmPrint = new FormPrinting.FormPrinting(this) ;
             frmPrint.Print(); 
             
             
         }

         private void panel65_Paint(object sender, PaintEventArgs e)
         {

         }

         private void checkBox80_CheckedChanged(object sender, EventArgs e)
         {

         }

         private void btnCalTHI_Click(object sender, EventArgs e)
         {
             CalTHI();
         }
    }
}
